/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.demo.module.config.sharding;

import com.demo.module.utils.ShardingUtils;
import com.google.common.collect.Range;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.time.LocalDateTime;
import java.util.*;

@Slf4j
public final class YearShardingAlgorithm implements StandardShardingAlgorithm<LocalDateTime>, ShardingAutoTableAlgorithm {



    @Getter
    private Properties props;

    @Getter
    private int autoTablesAmount;

    @Override
    public void init(final Properties props) {
        this.props = props;
    }

    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<LocalDateTime> preciseShardingValue) {
        LocalDateTime localDateTime = preciseShardingValue.getValue();
        String year = String.valueOf(localDateTime.getYear());
//        for (String ds : availableTargetNames) {
//            if (ds.endsWith(year)) {
//                return ds;
//            }
//        }
        if (localDateTime.getYear() == 2024) {
            return "ds0";
        } else {
            return "ds1";
        }
    }

    @Override
    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<LocalDateTime> rangeShardingValue) {
        Set<String> list = new HashSet<>();
        Range<LocalDateTime> valueRange = rangeShardingValue.getValueRange();
        LocalDateTime lowerDate = valueRange.lowerEndpoint();
        LocalDateTime upperDate = valueRange.upperEndpoint();
        String lowerSuffix = ShardingUtils.getSuffixByYearMonth(ShardingUtils.formatterYear, lowerDate.toLocalDate());
        String upperSuffix = ShardingUtils.getSuffixByYearMonth(ShardingUtils.formatterYear, upperDate.toLocalDate());
        Set<String> suffixList = ShardingUtils.getSuffixListForRangeDb(lowerSuffix, upperSuffix);
        for (String tableName : availableTargetNames) {
            if (containTableName(suffixList, tableName)) {
                list.add(tableName);
            }
        }
        return list;
    }

    @Override
    public String getType() {
        return "AUTO_CUSTOM";
    }



    private boolean containTableName(Set<String> suffixList, String tableName) {
        boolean flag = false;
        for (String s : suffixList) {
            if (tableName.endsWith(s)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}
